<template>
    <div class="manage">
        <el-dialog title="提示" :visible.sync="dialogVisible" :before-close="handleClose" width="50%">
            <!-- 展开dialog后显示用户表单 -->
            <el-form ref="form" :model="form" :inline="true" :rules="rules" label-width="80px">
                <el-form-item label="姓名" prop="name">
                    <el-input v-model="form.name" placeholder="输入姓名"></el-input>
                </el-form-item>
                <el-form-item label="年龄" prop="age">
                    <el-input v-model="form.age" placeholder="输入年龄"></el-input>
                </el-form-item>
                <el-form-item label="性别" prop="sex">
                    <el-select v-model="form.sex" placeholder="请选择性别">
                        <el-option label="男" :value="1"></el-option>
                        <el-option label="女" :value="0"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="出生日期" prop="birth">
                    <el-date-picker v-model="form.birth" type="date" placeholder="选择日期" value-format="yyyy-MM-DD">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="地址" prop="addr">
                    <el-input v-model="form.addr" placeholder="输入地址"></el-input>
                </el-form-item>
            </el-form>

            <span slot="footer" class="dialog-footer">
                <el-button @click="cancel">取 消</el-button>
                <el-button type="primary" @click="submit">确 定</el-button>
            </span>
        </el-dialog>
        <div class="manage-header">
            <el-button type="primary"  @click=handleAdd>
                + 新增
            </el-button>
            <!-- 搜索用户表单 -->
            <el-form :model="userForm" :inline="true">
                <el-form-item>
                    <el-input v-model="userForm.name" placeholder="请查询" ></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="onSubmit" >查询</el-button>
                </el-form-item>
            </el-form>
        </div>
        <div class="userList">
            <el-table :data="tableData" style="width: 100%; height:90%;" stripe>
                <el-table-column prop="name" label="姓名" width="180">
                </el-table-column>
                <el-table-column prop="age" label="年龄" width="180">
                </el-table-column>
                <el-table-column prop="sex" label="性别" width="180">
                    <template slot-scope="scope">
                        <span style="margin-left: 10px">{{ scope.row.sex == 0 ? '女' : '男' }}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="birth" label="出生日期" width="180">
                </el-table-column>
                <el-table-column prop="addr" label="地址">
                </el-table-column>

                <el-table-column>
                    <template slot-scope="scope">
                        <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
                        <el-button type="danger" size="mini" @click="handleDel(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pager">
                <el-pagination layout="prev, pager, next" :total="pagesLength" @current-change="handleCurrentPage">
                </el-pagination>
            </div>
        </div>
    </div>
</template>

<script>
import { getUser, editUser, addUser, delUser } from '../api'
export default {
    data() {
        return {
            dialogVisible: false,
            form: {
                name: '',
                age: '',
                sex: '',
                birth: '',
                addr: ''
            },
            rules: {
                name: [
                    { required: true, message: '请输入姓名' },
                ],
                age: [
                    { required: true, message: '请输入年龄' },
                ],
                sex: [
                    { required: true, message: '请输入性别' },
                ],
                birth: [
                    { required: true, message: '请输入出生日期' },
                ],
                addr: [
                    { required: true, message: '请输入地址' },
                ]
            },
            tableData: [],
            // 增加/编辑组件复用 1代表增加用户，2代表编辑用户
            modelType: 0,
            pagesLength: 0,// 页码总数
            pageData: {
                page: 1,
                limit: 10
            },
            userForm: {
                name: ''
            }
        }
    },
    methods: {
        //点击确定 成功->提交
        submit() {
            this.$refs.form.validate(valid => {
                if (valid) {
                    this.dialogVisible = false
                    if (this.modelType) {
                        addUser(this.form).then(() => {
                            this.getList()
                        })
                    } else {
                        editUser(this.form).then(() => {
                            this.getList()
                        })
                    }
                }
            })
        },
        // 这样取消按钮也能清空表单了
        cancel() {
            this.handleClose()
        },
        // 提交成功时 调用handleClose
        handleClose() {
            this.$refs.form.resetFields()
            this.dialogVisible = false
        },
        handleEdit(row) {
            this.modelType = 0
            this.dialogVisible = true
            this.form = JSON.parse(JSON.stringify(row))
        },
        handleDel(row) {
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                delUser({ id: row.id }).then(() => {
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                    this.getList()
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        handleAdd() {
            this.modelType = 1
            this.dialogVisible = true
        },
        // 重新加载用户列表
        getList() {
            getUser({ params: { ...this.pageData, ...this.userForm } }).then(({ data }) => {
                this.tableData = data.list
                this.pagesLength = data.count || 0
            })
        },
        //换页 并且更新页面数据
        handleCurrentPage(val) {
            this.pageData.page = val
            this.getList()
        },
        // 搜索框搜索数据
        onSubmit() {
            this.getList()
        }
    },
    mounted() {
        this.getList()
    },
    // 删除输入框后重新渲染信息表
    watch: {
        'userForm.name': {
            handler(newVal) {
                if(newVal==='') this.getList()
            },
            deep: true,
            immediate: true
        }
    }
}
</script>

<style lang="less" scoped>
.manage {
    height: 90%;
    .manage-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
    }

    .userList {
        position: relative;
        // calc减号左右一定要空格
        height: calc(100% - 62px);

        .pager {
            position: absolute;
            bottom: 0;
            right: 20px;
        }
    }
}
</style>